package com.example.multitenant.multitenant.config

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

/**
 * @author xiang
 * @Date 2022/9/3 20:50
 **/

@Component
@Order(value = 1)
class AfterApplicationRunner : ApplicationRunner {

    @Autowired
    private lateinit var dataSources: DataSources

    @Autowired
    private lateinit var tenantProperties: MultiTenantProperties

    override fun run(args: ApplicationArguments?) {
        TenantContext.setTenantId(tenantProperties.masterDb)
        dataSources.insertDefaultTenant()
        dataSources.initTenantDataSources()
    }

}